package com.serviceprovider.business.module.admin.settings;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.settings.model.DefaultFee;
import com.serviceprovider.business.module.admin.settings.model.SettingsVO;
import com.serviceprovider.business.module.admin.settings.services.ISettingsService;
import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.Int2DoublePropertyEditor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSession;

@Controller
public class SettingsController {
    protected final Log                 logger = LogFactory.getLog(getClass());
    @Autowired
    private ApplicationConfigProperties configProperties;
    @Autowired
    private ISettingsService            propertiesService;
    @Autowired
    private IUserService                userService;
    @Autowired
    @Qualifier("validator")
    private Validator                   validator;

    @ModelAttribute("uiThemes")
    public Map<String, String> getUiThemes() {
        return configProperties.getUiThemes();
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Integer.class, "newFee", new Int2DoublePropertyEditor(true));
        binder.registerCustomEditor(Integer.class, "allFees.fee", new Int2DoublePropertyEditor(true));
        binder.registerCustomEditor(Date.class, null,
                                    new CustomDateEditor(new SimpleDateFormat(configProperties.getDateFormat()),
                                        false));
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.GET
    )
    public ModelAndView getSettings() {
        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");
        SettingsVO   vo  = new SettingsVO();

        vo.setNewFee(0);
        vo.setNewEffectiveFrom(new Date());
        vo.setAllFees(propertiesService.getDefaultFees(new Date(0)));
        vo.setNewFee(propertiesService.getDefaultFee());
        vo.setUiTheme(propertiesService.getUiTheme());
        vo.setCheckPrintBy(propertiesService.getDefaultCheckPrintedBy());
        vo.setOrderApproval(propertiesService.getDefaultOrderApproval());
        mav.addObject("settings", vo);

        return mav;
    }

    /**
     * Save data from jsp page into database
     *
     * @param settings - Site settings to save
     * @return ModelAndView object pointed to site settings page
     */
    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "add"
    )
    public ModelAndView addDefaultFee(@ModelAttribute("settings") SettingsVO settings, BindingResult result) {
        validator.validate(settings, result);

        if (!result.hasErrors()) {
            propertiesService.createDefaultFee(new DefaultFee(settings.getNewFee(), settings.getNewEffectiveFrom()));

            return new ModelAndView("redirect:settings.htm");
        }

        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");

        mav.addObject("settings", settings);

        return mav;
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "theme"
    )
    public ModelAndView updateThemeUi(@ModelAttribute("settings") SettingsVO settings, HttpSession session) {
        propertiesService.updateUiTheme(settings.getUiTheme());
        propertiesService.setDefaultCheckPrintedBy(settings.getCheckPrintBy());
        propertiesService.setDefaultOrderApproval(settings.getOrderApproval());
        session.setAttribute("uiTheme", settings.getUiTheme());

        return new ModelAndView("redirect:settings.htm");
    }

    @RequestMapping(
        value    = "/admin/sitesettings/delete_fee.htm",
        method   = RequestMethod.GET
    )
    public ModelAndView deleteDefaultFee(@RequestParam(
        value    = "id",
        required = true
    ) Integer id) {
        ModelAndView mav = new ModelAndView("redirect:settings.htm");

        propertiesService.deleteDefaultFee(id);

        return mav;
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "spactive"
    )
    public ModelAndView activateSP(@ModelAttribute("settings") SettingsVO settings, BindingResult result) {
        userService.updateAllUsersActiveFlag(2, true);    // ServiceProvider, make active
        settings.setMessage("sp.activated");

        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");

        mav.addObject("settings", settings);

        return mav;
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "spinactive"
    )
    public ModelAndView deactivateSP(@ModelAttribute("settings") SettingsVO settings, BindingResult result) {
        userService.updateAllUsersActiveFlag(2, false);    // ServiceProvider, make inactive
        settings.setMessage("sp.deactivated");

        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");

        mav.addObject("settings", settings);

        return mav;
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "payeeactive"
    )
    public ModelAndView activatePayee(@ModelAttribute("settings") SettingsVO settings, BindingResult result) {
        userService.updateAllUsersActiveFlag(3, true);    // Payee, make active
        settings.setMessage("payee.activated");

        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");

        mav.addObject("settings", settings);

        return mav;
    }

    @RequestMapping(
        value  = "/admin/sitesettings/settings.htm",
        method = RequestMethod.POST,
        params = "payeeinactive"
    )
    public ModelAndView deactivatePayee(@ModelAttribute("settings") SettingsVO settings, BindingResult result) {
        userService.updateAllUsersActiveFlag(3, false);    // ServiceProvider, make inactive
        settings.setMessage("payee.deactivated");

        ModelAndView mav = new ModelAndView("admin/sitesettings/settings");

        mav.addObject("settings", settings);

        return mav;
    }
}



